//
//  OriginImageView.m
//  SNS
//
//  Created by Bobo on 11-12-6.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "OriginImageViewController.h"
#import "OriginImageView.h"

@implementation OriginImageViewController

@synthesize scrollView;
@synthesize imageView;
@synthesize gifView;
@synthesize doubleTapDelegate;
@synthesize doubleTapCallback;
@synthesize toolbar;
@synthesize saveImage;

- (void) viewDidLoad {
    [super viewDidLoad];
    
    self.toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 436, 320, 44)] autorelease];
    self.toolbar.barStyle = UIBarStyleBlackTranslucent;
    self.toolbar.hidden = YES;
    
    UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                                                          target:self
                                                                          action:@selector(confirmSaveImage)] autorelease];
    
    [self.toolbar setItems:[NSArray arrayWithObjects:item, nil]];
    
    [[UIApplication sharedApplication].keyWindow addSubview:self.toolbar];
    
    self.scrollView.tapDelegate = self;
    self.scrollView.tapCallback = @selector(showToolbar);
}

- (void)showToolbar { 
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    
    self.toolbar.hidden = NO;
    
    [self performSelector:@selector(hideToolbar)
               withObject:nil 
               afterDelay:4.0f];
    
}

- (void) hideToolbar {
    self.toolbar.hidden = YES;
}

- (void) confirmSaveImage {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片"
                                                    message:@"是否将图片保存到本地照片库？"
                                                   delegate:self
                                          cancelButtonTitle:@"取消"
                                          otherButtonTitles:@"确定", nil];
    [alert show];
    [alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        UIImageWriteToSavedPhotosAlbum(self.saveImage, nil, nil, nil);
    }
}

- (void) setImage:(UIImage *)image {
 
    self.saveImage = image;
    
    if (self.scrollView == nil) {
        self.scrollView = [[[OriginImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
        [self.view addSubview:self.scrollView];
        self.scrollView.delegate = self;
        self.scrollView.doubleTapDelegate = self.doubleTapDelegate;
        self.scrollView.doubleTapCallback = self.doubleTapCallback;
    }
    
    if (self.imageView != nil) {
        [self.imageView removeFromSuperview];
        self.imageView = nil;
    }
    if (self.gifView != nil) {
        [self.gifView removeFromSuperview];
        self.gifView = nil;
    }
    
    self.imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
    self.imageView.frame = CGRectMake(0,
                                      0, 
                                      image.size.width,
                                      image.size.height);
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = image.size;
    
    if (image.size.height < 480) {
        self.scrollView.contentOffset = CGPointMake(image.size.width/2 - 160, 
                                                    image.size.height/2 - 240);
    }
    else {
        self.scrollView.contentOffset = CGPointMake(image.size.width/2 - 160, 
                                                    0);
    }

    
    self.scrollView.maximumZoomScale = 2.0f;
    if (self.imageView.frame.size.width > 320.0f) {
        self.scrollView.minimumZoomScale = 320.0f / self.imageView.frame.size.width;
        self.scrollView.zoomScale = self.scrollView.minimumZoomScale;
    }
}

- (void) setGif:(NSData *)data {
    if (self.gifView != nil) {
        [self.gifView removeFromSuperview];
        self.gifView = nil;
    }
    
    if (self.scrollView != nil) {
        [self.scrollView removeFromSuperview];
        self.scrollView = nil;
    }

    
    UIImage *gifImage = [UIImage imageWithData:data];
    self.saveImage = gifImage;
    
    self.gifView = [[[UIWebView alloc] initWithFrame:CGRectMake(160 - gifImage.size.width/2,
                                                                240 - gifImage.size.height/2,
                                                                gifImage.size.width,
                                                                gifImage.size.height)] autorelease];
    [self.view addSubview:self.gifView];
    self.gifView.scrollView.scrollEnabled = NO;
    
    [self.gifView loadData:data
                  MIMEType:@"image/gif"
          textEncodingName:nil
                   baseURL:nil];
    
    if (self.scrollView == nil) {
        self.scrollView = [[[OriginImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
        [self.view addSubview:self.scrollView];
        self.scrollView.delegate = self;
        self.scrollView.doubleTapDelegate = self.doubleTapDelegate;
        self.scrollView.doubleTapCallback = self.doubleTapCallback;
    }
}

- (void) dealloc {
    [scrollView release];
    [imageView release];
    [gifView release];
    [toolbar release];
    [saveImage release];
    
    [super dealloc];
}

#pragma mark - UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

@end
